const App = {
	data() {
		return {
			isLoading: false,
			errMsg: '',
			loginInit: '/login/init',
			loginApi: '/cas/login?service=http%3A%2F%2Flocalhost%3A3000%2Finfoplus%2Fform%2FXNYQSB%2Fstart',
			startApi: '/infoplus/interface/start',
			lt: '',
			form: {
				username: '',
				password: '',
				isAutoClick: false
			}
		};
	},

	created() {
		this.isLoading = true;
		axios.get(this.loginInit)
		.then((res) => {
			console.log(res);
		})
		.catch((error) => {
			console.error(error);
			this.errMsg = "出错了, 看控制台";
		})
		.finally(() => {
			this.isLoading = false;
		});
	},

	methods: {
		onSubmit() {

			if (!this.form.username || !this.form.password) {
				this.errMsg = '账号和密码不可为空';
				return;
			}

			this.errMsg = '';

			this.isLoading = true;
			let ltGetPromise = Promise.resolve();
			if (!this.lt) {
				ltGetPromise = axios.get(this.loginApi)
				.then((res) => {
					let ltMatch = res.data.match(/value="(LT-.*?-cas)"/);
					if (!ltMatch) {
						return this.loginSuccess(res);
					}
					this.lt = ltMatch[1];
					return res;
				})
			}
			ltGetPromise
			.then(() => {
				return axios.post(this.loginApi, new URLSearchParams({
					// rsa是账号密码的加密形式
					rsa: strEnc(`${this.form.username}${this.form.password}${this.lt}`, "1", "2", "3"),
					// 账号长度 
					ul: this.form.username.length,
					// 密码长度
					pl: this.form.password.length,
					// 是动态的，是由get请求login.url获取中表单可获取
					lt: this.lt,
					isAutoClick: this.form.isAutoClick,
					execution: "e1s1",
					_eventId: "submit"
				}).toString())
				.then((res) => {
					let ltMatch = res.data.match(/value="(LT-.*?-cas)"/);
					if (ltMatch) {
						this.lt = ltMatch[1];
						this.errMsg = "账号或密码错误, 请检查输入"
						return;
					}
					return this.loginSuccess(res);
				})
			})
			.catch((error) => {
				console.error(error);
				this.errMsg = "出错了, 看控制台";
			})
			.finally(() => {
				this.isLoading = false;
			});
		},
		loginSuccess(res) {
			return new Promise((resolve, reject) => {
				let tokenMatch = res.data.replace(/ /g, '').match(/<metaitemscope="csrfToken"content="(.*?)">/);
				if (!tokenMatch) {
					return reject(`${res.data}匹配token失败, 请检查正则表达式`);
				}
				this.isLoading = true;
				return axios.post(this.startApi, new URLSearchParams({
					// 这可能是企业微信嵌入应用的id号，打开id不变，写死就可以了
					idc: "XNYQSB",
					release: "",
					"csrfToken": tokenMatch[1],
					// formData: {"fieldCXY":"全国疫情中高风险地区名单（截至2021年11月01日08时）\n高风险地区：\n内蒙古自治区阿拉善盟额济纳旗达来呼布镇\n北京市昌平区北七家镇宏福苑社区\n\n中风险地区：\n北京市昌平区东小口（地区）镇森林大第家园社区\n内蒙古自治区呼和浩特市新城区华侨新村小区\n内蒙古自治区呼和浩特市金川开发区碧水蓝山小区\n内蒙古自治区锡林郭勒盟二连浩特市锡林社区\n内蒙古自治区锡林郭勒盟二连浩特市西城社区\n内蒙古自治区锡林郭勒盟二连浩特市额仁社区\n内蒙古自治区阿拉善盟阿拉善左旗巴彦浩特镇新华街道\n黑龙江省哈尔滨市平房区绿色新城西区\n黑龙江省哈尔滨市平房区国际花都一期\n黑龙江省哈尔滨市香坊区恒大时代广场一期\n黑龙江省黑河市爱辉区长海社区盛泰小区\n黑龙江省黑河市爱辉区人保财险社区林业集资楼小区\n黑龙江省黑河市爱辉区鹿源春社区宏志小区\n黑龙江省黑河市爱辉区博文社区昌辉雅典城小区\n黑龙江省黑河市爱辉区博文社区尚品佳苑小区\n黑龙江省黑河市爱辉区长海社区中房76号楼小区\n江西省上饶市铅山县葛仙山镇\n山东省日照市五莲县洪凝街道学府壹号小区\n贵州省遵义市汇川区仁和苑小区\n贵州省遵义市汇川区东方湘江湾小区\n甘肃省兰州市城关区云祥小区\n甘肃省兰州市城关区雁北路天庆丽舍小区\n甘肃省兰州市城关区东湖小区\n甘肃省张掖市甘州区锦绣嘉苑（梁家墩镇六号村）\n甘肃省张掖市甘州区龙王庙小区\n宁夏回族自治区银川市兴庆区太阳都市花园小区\n宁夏回族自治区银川市西夏区物华兴洲苑小区\n宁夏回族自治区银川市金凤区森林半岛小区","fieldCXY2":"","fieldYQTP":"http://202.192.16.87:8080/api/get_dk_img","_VAR_ACTION_REALNAME":"刘佳合","_VAR_RELEASE":"true","_VAR_NOW_MONTH":"5","_VAR_ACTION_USERCODES":"1865400006","_VAR_ACTION_ACCOUNT":"1865400006","_VAR_ACTION_ORGANIZES_Names":"计算机科学与网络工程学院","_VAR_URL_Attr":"{}","_VAR_POSITIONS":"0206:02:1865400006","_VAR_ACTION_ORGANIZES_Codes":"0206","_VAR_NOW_YEAR":"2022","_VAR_ACTION_INDEP_ORGANIZES_Codes":"02","_VAR_ACTION_ORGANIZE":"0206","_VAR_ACTION_INDEP_ORGANIZE":"02","_VAR_ACTION_INDEP_ORGANIZE_Name":"学院","_VAR_ACTION_ORGANIZE_Name":"计算机科学与网络工程学院","_VAR_OWNER_ORGANIZES_Codes":"0206","_VAR_ADDR":"113.87.162.153","_VAR_OWNER_ORGANIZES_Names":"计算机科学与网络工程学院","_VAR_URL":"https://yqtb.gzhu.edu.cn/infoplus/form/XNYQSB/start","_VAR_ACTION_INDEP_ORGANIZES_Names":"学院","_VAR_OWNER_ACCOUNT":"1865400006","_VAR_OWNER_USERCODES":"1865400006","_VAR_NOW_DAY":"8","_VAR_OWNER_REALNAME":"刘佳合","_VAR_NOW":"1652000045","_VAR_ENTRY_NUMBER":"-1","_VAR_ENTRY_NAME":"","_VAR_ENTRY_TAGS":""},
					lang: "zh"
				}).toString())
				.then((res) => {
					window.open(res.data.entities[0].replace(new URL(res.data.entities[0]).origin, ''), '_blank');
					// window.open(res.data.entities[0].replace(new URL(res.data.entities[0]).origin, ''), '_self');
					return res;
				}).finally(() => {
					this.isLoading = false;
				});
			})
		}
	}
};
const app = Vue.createApp(App);
app.use(ElementPlus);
app.mount("#app");
